package cn.tedu.web.stm;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import java.lang.reflect.Field;

public class AppTest {
    public static void main(String[] args) {
        Animal a = gen(Animal.class);
        Bird b = gen(Bird.class);
        System.out.println(a);
        System.out.println(b);
    }
    public static <X> X gen(Class<X> s) {
        try {
            X x = s.newInstance();
            Field[] fields = s.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                Class<?> type = field.getType();
                if (type.isAssignableFrom(String.class)) {
                    String value = RandomStringUtils.randomAlphabetic(3);
                    field.set(x, value);
                } else if (type.isAssignableFrom(Double.class)) {
                    double v = RandomUtils.nextDouble();
                    field.set(x, v);
                }
            }
            return x;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
